import { Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    MaterialIntoServiceProxy,
    CreateOrUpdateMaterialDto,
    LocationServiceProxy,
    CreateOrUpdateMaterialForProductInput,
    AppLicationTreeDto,
    MaterialForProductEditDto,
    InboundInventoryOfMaterialInput
} from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import * as moment from 'moment';

@Component({
    selector: 'createOrEditWlModal',
    styleUrls: ['./create-or-edit-wl-modal.component.less'],
    templateUrl: './create-or-edit-wl-modal.component.html'
})
export class CreateOrEditWlModalComponent extends AppComponentBase {

    @ViewChild('createOrEditModal') modal: ModalDirective;

    @ViewChild('billDatePicker') billDatePicker: ElementRef;
    @ViewChild('scanTimePicker') scanTimePicker: ElementRef;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    productionDate = new Date();
    storageDateEnd = new Date();
    recheckValidDateFrom = new Date();
    recheckValidDateEnd = new Date();
    wl = new CreateOrUpdateMaterialForProductInput();
    CreateOrUpdateMaterialDto = new CreateOrUpdateMaterialDto();
    // areas = new Array<AppLicationTreeDto>();
    // area = new AppLicationTreeDto();
    positionId = "";
    constructor(
        injector: Injector,
        private _warehouseIntoServiceProxy: MaterialIntoServiceProxy,
        private _locationServiceProxy: LocationServiceProxy,
    ) {
        super(injector);
    }

    show(): void {
        const self = this;
        self.active = true;
        this.wl.materialForProduct = new MaterialForProductEditDto();
        // this._locationServiceProxy.getAreas("", true, 51)
        //     .subscribe(result => {
        //         this.areas = result.items;
        //     });
        self.modal.show();

    }

    save(): void {
        //const self = this;
        //this.wl.outStockBill.scanTime = moment(this.scanTime);
        if (!this.positionId) {
            this.notify.error('请选择一个位置');
            return;
        }
        this.CreateOrUpdateMaterialDto.createOrUpdateMaterialForProductInput = new CreateOrUpdateMaterialForProductInput();
        this.CreateOrUpdateMaterialDto.inboundInventoryOfMaterialInput = new InboundInventoryOfMaterialInput();
        this.CreateOrUpdateMaterialDto.inboundInventoryOfMaterialInput.locationOfMaterialId = this.positionId;
        this.wl.materialForProduct.goodsStatus = 0;
        this.wl.materialForProduct.departmentId = 51;
        this.wl.materialForProduct.productionDate = moment(this.productionDate);
        this.wl.materialForProduct.storageDateEnd = moment(this.storageDateEnd);
        this.wl.materialForProduct.recheckValidDateFrom = moment(this.recheckValidDateFrom);
        this.wl.materialForProduct.recheckValidDateEnd = moment(this.recheckValidDateEnd);
        this.CreateOrUpdateMaterialDto.createOrUpdateMaterialForProductInput = this.wl;
        this.saving = true;
        this._warehouseIntoServiceProxy.createOrUpdateReleaseStockBill(this.CreateOrUpdateMaterialDto)
            .pipe(finalize(() => this.saving = false))
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
